package org.jiuling.appletofwechat;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.jiuling.appletofwechat.enums.AppletOfWeChatApiEnum;
import org.jiuling.appletofwechat.model.JsCode2SessionModel;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.network.tools.http.request.HttpRequest;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.model.HttpRequestParam;
import org.jiuling.network.tools.response.ResponseData;
import org.jiuling.wechat.config.WeChatConfig;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

/**
 * 微信小程序登录
 * @author 紫灵
 * @date 2024年01月28日 22时45分01秒
 */
public class AppletOfWeChatLogin {

    /** 微信配置 */
    private final WeChatConfig weChatConfig;

    public AppletOfWeChatLogin(WeChatConfig weChatConfig) {
        this.weChatConfig = weChatConfig;
    }

    /**
     * JS验证码转Session
     * @param jsCode JS验证码
     * @return JS验证码转Session模型
     */
    public ResponseData<JsCode2SessionModel> jsCode2Session(String jsCode){
        AppletOfWeChatApiEnum jsCode2SessionApi = AppletOfWeChatApiEnum.JS_CODE_2_SESSION;
        // 封装请求参数
        HttpRequestParam httpRequestParam = new HttpRequestParam()
                .add("appid",weChatConfig.getAppId())
                .add("secret",weChatConfig.getAppSecret())
                .add("js_code",jsCode)
                .add("grant_type","authorization_code");
        try {
            HttpRequest httpRequest = new HttpRequest();
            // 获取开启HTTP请求过程
            HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory(jsCode2SessionApi.getApi());
            httpRequestFactory.setRequestParam(httpRequestParam);
            httpRequestFactory.setRequestMethod(jsCode2SessionApi.getRequestMethod());
            JSONObject jsonObject = JSON.parseObject(httpRequest.send().getBody());
            JsCode2SessionModel jsCode2SessionModel =  JsCode2SessionModel.builder()
                    .addOpenId(jsonObject.getString("openid"))
                    .addSessionKey(jsonObject.getString("session_key"))
                    .addUnionId(jsonObject.getString("unionid"))
                    .addErrCode(jsonObject.getInteger("errcode"))
                    .addErrMsg(jsonObject.getString("errmsg"))
                    .build();
            return new ResponseData<>(
                    !jsonObject.containsKey("errcode")?0:jsonObject.getInteger("errcode"),
                    !jsonObject.containsKey("errmsg")?null:jsonObject.getString("errmsg"),
                    jsCode2SessionModel
            );
        } catch (RequestException | IOException | LocalToolsException | NoSuchAlgorithmException |
                 KeyManagementException e) {
            throw new RuntimeException(e);
        }

    }
}
